package xhm;

import  java.io.IOException;
import java.util.HashMap;
import java.util.Scanner;

public class BankSystem {
    HashMap<String, Save> bankdb;//String键，唯一，Save储户信息
    Scanner input;
    public BankSystem() {//构造函数，作用初始化数据成员
        bankdb=new HashMap<>();
        input=new Scanner(System.in);
    }// // this.bankdb = bankdb;

    public void runBankSystem() throws IOException, InterruptedException {
        String flags="N";
        do {
            BankSystemMenu.showMenu();
            String userInput = input.next();
            switch (userInput) {
                case "1" -> {
                    createAccount();
                }
                case "2" -> {
                    deleteAccount();
                }
                case "3" -> {
                    saveMoney();
                }
                case "4" -> {
                    getMoney();
                }
                case "5" -> {
                    changeMoney();
                }
                case "6" -> {
                    showBalance();
                }
                case "7" -> {
                    exit();
                }
                default -> {
                    System.out.println("您输入有误，请重试");
                }
            }
            System.out.println("是否继续操作本系统？(Y|N)");
            flags=input.next();
        }while (flags.equalsIgnoreCase("y"));
        System.out.println("欢迎下次使用");
    }

    public void createAccount() {
        System.out.println("正在创建账户...");
        System.out.println("请输入用户名");
        String name=input.next();
        System.out.println("请输入密码：");
        String pwd=input.next();
        System.out.println("请输入性别");
        String gender=input.next();
        Save save=new Save();
        String accountid="6226"+(int)(1000+(Math.random()*9000));
        System.out.println("以为您自动生成一个卡号:"+accountid);
        save.setAccountid(accountid);
        save.setName(name);
        save.setPwd(pwd);
        save.setGender(gender);
        bankdb.put(accountid,save);//银行账号
        System.out.println("您已完场开户");
        System.out.println("您的开户信息："+bankdb);
    }

    public void saveMoney() {
        System.out.println("");
    }

    public void getMoney() {

    }
    public void changeMoney(){

    }
    public  void showBalance(){
        System.out.println("查询余额" );
    }
    public void deleteAccount() throws  InterruptedException {
        boolean iscontinue=false;
        int count=3;
        System.out.println("正在执行销户。。。");
        System.out.println("。。。。。。。。。。。。。");
        System.out.print("请输入您的银行账号");
        String account=input.next();
        if(bankdb.containsKey(account)){
            Save save=bankdb.get(account);
            do {
                System.out.print("请输入您的密码：");
                String pwd = input.next();
                if (pwd.equals(save.getPwd())) {
                    System.out.println("正在销户。请稍后。。。");
                    Thread.sleep(1000);//等一秒钟
                    bankdb.remove(account);
                    System.out.println("销户完成！");
                    iscontinue = false;
                } else {
                    //可以添加的机会次数
                    count--;
                    if (count == 0) {
                        System.out.println("您的密码次数已用完,即将退出销户功能");
                        Thread.sleep(1000);
                        return;
                    }
                    System.out.printf("您的密码输入有误，还剩下" + count + "次机会，请重试");
                    iscontinue = true;
                }
            }while (iscontinue) ;
            }else{
                System.out.println("您的银行账户输入有误！请重试。");//可以重复几次
             }
    }
    public void exit(){
       System.out.println("是否真的要退出系统？(Y|N):");
       String userinput=input.next();
       if(userinput.equalsIgnoreCase("y")){
           System.exit(0);
       }
    }
}



